package com.jollychic.services;

import com.jollychic.dao.slave.SlaveUserMapper;
import com.jollychic.dao.master.MasterUserMapper;
import com.jollychic.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by xusheng on 2019/4/18.
 */
@Transactional
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    MasterUserMapper masterUserDao;
    @Autowired
    SlaveUserMapper slaveUserDao;

    @Override
    public User addUserMaster(User user) {
        //master添加数据
        int ret= masterUserDao.addUser(user.getUserName(),user.getUserId());
        if (ret>0 ){
            user.setUserName(user.getUserName()+":master添加数据");
            return user;
        }
        return null;
    }

    @Override
    public User addUserSlave(User user) {
        //slave添加数据
        int ret= slaveUserDao.addUser(user.getUserName(),user.getUserId());
        //int ret=1;
        if (ret>0 ){
            user.setUserName(user.getUserName()+":slave添加数据");
            return user;
        }
        return null;
    }

    /**
     * 多数据源，分布式事务回滚
     * 用jta+atomikos实现，有时间再做
     * @param user
     * @return
     */
    @Override
    public User addUserTran(User user) {
        masterUserDao.addUser(user.getUserName(),user.getUserId());
        slaveUserDao.addUser(user.getUserName(),user.getUserId());

        int i=10/0;
        return user;
    }

    //    @Override
////    public User addUser(User user) {
////        //return userDao.addUser(user);
////        return null;
////    }
}
